package com.aaa.javaweb.servlet;

import com.aaa.javaweb.dao.impl.LogDaoImpl;
import com.aaa.javaweb.entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;


@WebServlet("/login")
public class LogServlet extends HttpServlet {
    LogDaoImpl logDao = new LogDaoImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html;charset=utf-8");
        String method = req.getParameter("method");
        if(method!=null){
            switch (method){
                case "loginOut":
                    loginOut(req,resp);
                    break;
            }
        }else{
            String name = req.getParameter("username");
            String pwd = req.getParameter("p");
            List<Map> query = logDao.query(name, pwd);

            HttpSession session = req.getSession();
            String imgpath = query.get(0).get("imgpath")+"";
            session.setAttribute("imgpath",imgpath);
            String id = query.get(0).get("id")+"";
            session.setAttribute("id",id);
            String spwd = query.get(0).get("spwd")+"";
            session.setAttribute("spwd",spwd);

            System.out.println();
            if(query.size()>0){
                if(query.get(0).get("status").equals(0)){
                    Map map = query.get(0);
                    User user=new User((Integer) map.get("id"),(String) map.get("sname"),(String) map.get("spwd"),(String) map.get("idcard"),
                            (String) map.get("imgpath"),(Integer) map.get("status"));
                    session.setAttribute("nowStaff",user);
                    session.setAttribute("name", name);
                    session.setAttribute("pwd", pwd);
                    session.setAttribute("user",query);

                    getAllUser(req, resp);
                }else{
                    resp.sendRedirect("index.jsp");
                }


            }else{
                resp.sendRedirect("index.jsp");
            }
        }



    }

    private void loginOut(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.invalidate();  //失效
        resp.sendRedirect("index.jsp");

    }

    public void getAllUser(HttpServletRequest req,HttpServletResponse resp) throws ServletException, IOException {
        //session
        HttpSession session = req.getSession();

        String name = session.getAttribute("name")+"";
        //String imgpath = session.getAttribute("imgpath")+"";
        //System.out.println("==="+imgpath);
        if(name==null){
            //跳转到登录页面
            resp.sendRedirect("index.jsp");

        }else {
            req.setAttribute("name",name);
            req.getRequestDispatcher("Meua/querymeua").forward(req, resp);

        }
    }

}

